package com.appheader.rongmaphone.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.appheader.rongmaphone.R;
import com.appheader.rongmaphone.entity.CloudEntity;
import com.appheader.rongmaphone.utils.ScreenUtils;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by zhep on 2016/9/26.
 */
public class CloudAdapter extends BaseAdapter {
    private List<CloudEntity> list;
    private Context context;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    private PullToRefreshGridView gridView;


    public CloudAdapter(List<CloudEntity> list, Context context,DisplayImageOptions options,ImageLoader imageLoader,PullToRefreshGridView gridView) {
        this.list = list;
        this.context = context;
        this.options = options;
        this.imageLoader = imageLoader;
        this.gridView = gridView;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CloudViewHolder holder = null;
        if (convertView == null) {
        holder = new CloudViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.main_item, parent, false);
        holder.img = (RoundedImageView) convertView.findViewById(R.id.cloud_imageView);
        holder.textView = (TextView) convertView.findViewById(R.id.cloud_textView);
            convertView.setTag(holder);
        } else {
            holder = (CloudViewHolder) convertView.getTag();
        }
        String name = list.get(position).getName();
        String id = list.get(position).getId();
//        String lastModifyTime = list.get(position).getLastModifyTime();
        holder.textView.setText(name);
        String imgPath = list.get(position).getPicId();

        ViewGroup.LayoutParams layoutParams = holder.img.getLayoutParams();
        //获得的
        layoutParams.height = ScreenUtils.getScreenHeight(context)/2;//一屏幕显示两行
        layoutParams.width = (ScreenUtils.getScreenWidth(context)-20)/2;//一屏幕显示两列
        holder.img.setLayoutParams(layoutParams);
//        Log.e("CloudAdapter", "getView: " + name + id + lastModifyTime + imgPath);
        //建立实体类存入数据库
        if (!TextUtils.isEmpty(imgPath)) {
            //用picasso做加载图片方法
//            Bitmap bitmap = FileUtils.redImg(imgPath);
//            if (bitmap == null){
            //方法一、
//            Picasso.with(context).load(imgPath).resize(250, 400).into(holder.img);

//            }else {
//                holder.img.setImageBitmap(bitmap);
//            }

            //方法二
            imageLoader.displayImage(imgPath,holder.img,options);
        }
        return convertView;
    }
    class CloudViewHolder {
        private RoundedImageView img;
        private TextView textView;


    }
}
